
#include<algorithm>
#include<iostream>
#include<cstring>
using std::cout;
using std::endl;
namespace ddsm
{
    class string
    {
    public:
        void swap(ddsm::string& s)
        {
            std::swap(_size,s._size);
            std::swap(_capacity,s._capacity);
            std::swap(_array,s._array);
        }
        //迭代器设计
        using iterator = char*;
        using const_iterator = const char*;
        iterator begin()
        {
            return _array;
        }
        iterator end()
        {
            return _array + _size;
        }
        const_iterator begin() const
        {
            return _array;
        }
        const_iterator end() const
        {
            return _array + _size;
        }
        //构造函数
        string(const char* str = "")//default给空串
            :_size(strlen(str))
            ,_capacity(_size)
            ,_array(new char[_size + 1])
        {
            strcpy(_array,str);
        }
        //拷贝构造现代写法
        string(const ddsm::string& s)
            :_array(nullptr)
        {
            ddsm::string tem(s._array);//复用构造函数
            ddsm::string::swap(tem);
        }
        //复用拷贝构造
        ddsm::string& operator=(string tem)//局部对象，出作用域析构
        {
            swap(tem);
            return *this;
        }
        void Print()
        {
            for(int i = 0;i < _size;++i)
            {
                cout<<*(_array+i);
            }
            cout<<endl;
        }
        //保留一定长度n，只增不减
        void reserve(int n)
        {
            if(n > _capacity)
            {
                char* tem = new char[n + 1];
                strcpy(tem,_str);
                delete[] _str;
                _str = tem;
                _capacity = n;
                //reserve不改变size
            }
        }
        void resize(int n)
        {
            if(n < _size)
            {
                _array[n] = '\0';
                _size = n;
            }
            else
            {
                if(n > _capacity)
                {
                    reserve(n);
                }
                int i = _size;
                while(i != n)
                {
                    _array[i++] = '\0';
                }
                _size = n;
                _array[_size] = '\0';
            }
        }
        void push_back(const char ch)
        {
            if(_size == _capacity)
            {
                int NewCapacity = (_capacity == 0 ? 4 : _capacity * 2);
                reserve(NewCapacity);
            }
            _array[_size++] = ch;
            _array[_size] = '\0';
        }
        string& append(const string& str)
        {
            int len = strlen(str);
            if (len + _size > _capacity)
            {
                reserve(len + _size);//不改变size
            }
            //扩容完毕
            strcpy(_str + _size, str);
            _size += len;

            return *this;
        }
        ~string()
        {
            if(_array)
                delete[] _array;
            _capacity = _size = 0;
            _array = nullptr;
        }
    private:
        size_t _size;//实际存储字符串的长度,这个长度不包含'\0'
        size_t _capacity;//实际开辟的空间的长度
        char* _array;//指向堆区空间的指针
    };
};

int main()
{
    ddsm::string str1("hello");
    ddsm::string str2(str1);
    ddsm::string str3;
    str3 = str2;
    str1.Print();
    str2.Print();
    str3.Print();

    return 0;
}